<?php
class Admin_SettlementController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        
        $tWhere = ' 1 and s.leave_status = 2'; 
        if(!empty($pDepartment)){
            $tWhere .= ' and s.department = '.$pDepartment;
        }

        $tSMO = new SeniorModel;
        $tDIMO = new DepartmentinfoModel;
        $tMIMO = new MedicalinsuranceModel;
        $tPIMO = new PaymentinfoModel;
        $tDsMO = new DiscountinfoModel;
        $tSmMO = new SettlementModel;
        $tLMO = new LeaveModel;
        $tAMO = new AdminModel;
        $tWMO = new WorkerModel;

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and s.leave_status = 2 and s.department = '.$tAData[0]['department'];
            }
        }

        $tTime = time();
        $tDay = date('t',strtotime(date('Y-m-d',$tTime)));

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID错误'):Tool_Fnc::safe_string($p['sid']);
            $pFee = empty($p['fee'])?Tool_Fnc::ajaxMsg('付费金额不能为空'):Tool_Fnc::safe_string($p['fee']);

            $tData = array(
                'sid' => $pSid,
                'fee' => $pFee,
                'is_pay' => 1,
                'created' => $tTime,
            );
            if(!$tSmMO->insert($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            Tool_Fnc::ajaxMsg('操作成功',1);
        }

        $tSql = 'select count(s.id) c from '.$tSMO->table.' s left join '.$tMIMO->table.' mi on s.id = mi.sid left join '.$tPIMO->table.' pi on pi.num = mi.payment left join '.$tDsMO->table.' ds on mi.charge_discount = ds.num left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere;
        $tCount = $tSMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tDatas = array();
        if(!empty($tCnt)){
            $tSql = 'select s.id id,s.name name,s.created created,mi.basic_fee basic_fee,pi.introduce payment,pi.num payment_num,ds.introduce discount,ds.num discount_num,di.introduce department from '.$tSMO->table.' s left join '.$tMIMO->table.' mi on s.id = mi.sid left join '.$tPIMO->table.' pi on pi.num = mi.payment left join '.$tDsMO->table.' ds on mi.charge_discount = ds.num left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere.' order by s.id desc limit '.$tPage->limit();
            $tDatas = $tSMO->query($tSql);
            
            if(!empty($tDatas)){
                foreach($tDatas as $tK=>$tV){
                    //计算总支付
                    $tCount = $tDatas[$tK]['discount'] * 0.1;
                    $tBasicfee = round($tV['basic_fee']/$tDay,2);
                    if($tV['discount_num'] == 1){
                        $tCount = 1;
                    }
                    //判断支付方式
                    if($tV['payment_num'] == 1){#日付
                        $tDatas[$tK]['total_fee'] = $tBasicfee * $tCount;
                    }else if($tV['payment_num'] == 2){#周付
                        $tDatas[$tK]['total_fee'] = $tBasicfee * 7 * $tCount;
                    }else if($tV['payment_num'] == 3){#月付
                        $tDatas[$tK]['total_fee'] = $tV['basic_fee'] * $tCount;
                    }else if($tV['payment_num'] == 4){#季付
                        $tDatas[$tK]['total_fee'] = $tV['basic_fee'] * 3 * $tCount;
                    }else if($tV['payment_num'] == 5){#年付
                        $tDatas[$tK]['total_fee'] = $tV['basic_fee'] * 12 * $tCount;
                    }
                    //是否结算
                    $tSmData = $tSmMO->field('is_pay,fee')->where('sid = '.$tV['id'])->fRow();
                    $tDatas[$tK]['is_pay'] = empty($tSmData['is_pay'])?0:1;
                    $tDatas[$tK]['fee'] = empty($tSmData['fee'])?0:$tSmData['fee'];
                    //获取离院时间
                    $tLData = $tLMO->field('created leavetime')->where('sid = '.$tV['id'])->fRow();
                    $tDatas[$tK]['leavetime'] = $tLData['leavetime'];
                }
            }
        }

        //部门显示
        $tDIDatas = $tDIMO->field('*')->fList();

        $this->assign('tShow',$tPage->show());
        $this->assign('pDepartment',$pDepartment);
        $this->assign('tDatas',$tDatas);
        $this->assign('tDIDatas',$tDIDatas);
    }
}
?>
